import React, {Component} from 'react';
import {NavigationActions} from 'react-navigation';
import StorageUtil from '../utils/StorageUtil';

import {
    Animated,
    Dimensions,
    Image,
    StatusBar,
    TouchableOpacity,
    View,
    StyleSheet,
    Text
} from 'react-native';
const {width,height} = Dimensions.get('window');

export default class SplashScreen extends Component {
    constructor(props) {
        super(props);
        this.state = {
            fadeAnim: new Animated.Value(0),
            hasLogin: false
        };
    }

    componentDidMount() {
        StorageUtil.get('hasLogin', (error, object) => {
            if (!error && object != null && object.hasLogin) {
                this.setState({hasLogin: object.hasLogin});
                this.autoLogin();
            }
        });
    }

    autoLogin(){
        const resetAction = NavigationActions.reset({
            index:0,
            actions:[
                NavigationActions.navigate({routeName:'Home'})]
        });
        this.props.navigation.dispatch(resetAction);
    }

    goLogin(){
        console.log('点击出现');
        // this.props.navigation.navigate('LocalLoginScreen')
        const resetAction = NavigationActions.reset({
            index:0,
            actions:[
                NavigationActions.navigate({routeName:'LocalLoginScreen'})]
        });
        this.props.navigation.dispatch(resetAction);
    }

    render() {
        return (
            <View>
                {/*<StatusBar backgroundColor="red"/>*/}
                <StatusBar
                    backgroundColor="#394f8b"
                    barStyle="light-content"
                />
                <Image source={require("../../images/lanch.png")}
                       style={{width: width, height: height}}/>
                {
                    this.state.hasLogin ? (null) : (
                        <Animated.View style={[styles.buttonContainer, {opacity: this.state.fadeAnim}]}>
                            <TouchableOpacity style={{flex: 1}} activeOpacity={0.6} onPress={() => {
                                this.props.navigation.navigate('LocalLoginScreen')
                            }}>
                                <View style={[styles.btnLogin, styles.btnColumn]}>
                                    <Text style={styles.button}>登录</Text>
                                </View>
                            </TouchableOpacity>
                            {/*<TouchableOpacity style={{flex: 1}} activeOpacity={0.6} onPress={() => {*/}
                                {/*this.props.navigation.navigate('Register')*/}
                            {/*}}>*/}
                                {/*<View style={[styles.btnRegister, styles.btnColumn]}>*/}
                                    {/*<Text style={[styles.button, {color: '#FFFFFF'}]}>注册</Text>*/}
                                {/*</View>*/}
                            {/*</TouchableOpacity>*/}
                        </Animated.View>
                    )
                }
                {/*{*/}
                    {/*this.state.hasLogin?(null):(*/}
                        {/*<TouchableOpacity activeOpacity={1} onPress={this.goLogin.bind(this)}>*/}
                            {/*<Image source={require("../../images/splash.png")} style={{width:width,height:height}}/>*/}
                        {/*</TouchableOpacity>*/}
                    {/*)*/}
                {/*}*/}
            </View>
        );
    }
}

const styles = StyleSheet.create({
    buttonContainer: {
        position: 'absolute',
        bottom: 50,
        flex: 1,
        flexDirection: 'row',
        paddingLeft: 20,
        paddingRight: 20,
    },
    btnColumn: {
        flex: 1,
        paddingLeft: 10,
        paddingRight: 10,
        justifyContent: 'center',
        alignItems: 'center',
        borderRadius: 3,
    },
    button: {
        flex: 1,
        paddingTop: 10,
        paddingBottom: 10,
        fontSize: 16,
    },
    btnLogin: {
        backgroundColor: 'red',
        marginRight: 15,
    },
    btnRegister: {
        backgroundColor: '#00BC0C',
        marginLeft: 15,
    }
});